package club.emergency.wechat.wechat_api.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.wechat_api.consumer.service.WechatApiConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.wechat_api.consumer.controller
 * Date: 2018/11/7
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api
@RestController
@RequestMapping("/wechat-basic")
public class WechatApiController {
    private WechatApiConsumer wechatApiConsumer;

    public WechatApiController(WechatApiConsumer wechatApiConsumer) {
        this.wechatApiConsumer = wechatApiConsumer;
    }

    @ApiOperation("根据code获取openId")
    @ApiImplicitParam(value = "code", name = "code", required = true, dataType = "string", paramType = "query")
    @PostMapping("/getOpenidByCode")
    public ResponseEntity getOpenIdByCode(
            @RequestParam("code") String code
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(wechatApiConsumer.getOpenIdByCode(code), ResponseEntity.class);
    }

    @ApiOperation("上传图文消息内的图片获取URL")
    @PostMapping("/uploadImageReturnUrl")
    public ResponseEntity uploadImageReturnUrl(MultipartFile file) throws IOException {
        return new JsonConvert<ResponseEntity>().jsonDecode(wechatApiConsumer.uploadImageReturnUrl(file.getOriginalFilename(), file.getBytes()), ResponseEntity.class);
    }

    @ApiOperation("获取关注的用户信息列表")
    @PostMapping("/getUserOpenIdList")
    public ResponseEntity getUserOpenIdList() {
        return new JsonConvert<ResponseEntity>().jsonDecode(wechatApiConsumer.getUserOpenIdList(), ResponseEntity.class);
    }

    @ApiOperation("根据OpenId获取人员详细信息")
    @ApiImplicitParam(value = "openId", name = "openId", required = true, dataType = "string", paramType = "query")
    @GetMapping("/getUserInfoByOpenId")
    public ResponseEntity getUserInfoByOpenId(
            @RequestParam("openId") String openId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(wechatApiConsumer.getUserInfoByOpenId(openId), ResponseEntity.class);
    }
}
